package com.dp.appi.service.impl;

import com.dp.appi.dao.ProjectLogDao;
import com.dp.appi.service.ProjectLogService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bchen on 2016/5/25.
 */

@Service
@Transactional(rollbackFor=Exception.class)
public class ProjectLogServiceImpl implements ProjectLogService {
    @Autowired
    private ProjectLogDao projectLogDao;

    /**
     * 项目日志搜索
     *
     * @param param
     * @param users
     * @return
     */
    @Override
    public List<Map<String, Object>> logList(Map<String, String> param, String users) {
        users = users.replaceAll("\\|","','");
        List<Map<String, Object>> logList = projectLogDao.logList(param,users);
        for(Map<String,Object> log :logList){
            long log_id = Long.parseLong(String.valueOf(log.get("log_id")));
            log.put("project",projectLogDao.logDetailList(log_id));
        }
        return logList;
    }

    /**
     * 项目日志填报
     voice_remark
     characters_remark
     stage_id
     project_statis
     * @param user_id
     * @param param
     */
    @Override
    public void assessedLog(String user_id, Map<String, String> param) {
        String project = param.get("project");
        String voice_remark = param.get("voice_remark");
        String characters_remark = param.get("characters_remark");
        Map<String,Object>  paramMap = new HashMap<>();
        paramMap.put("voice_remark",voice_remark);
        paramMap.put("characters_remark",characters_remark);
        paramMap.put("user_id",user_id);
        Long log_id = projectLogDao.assessedLog(paramMap);


        List<Map<String,Object>> proLogList = new Gson().fromJson(project, ArrayList.class);
        for(Map<String,Object> proLog : proLogList){
            Long stage_id = Math.round(Double.parseDouble(String.valueOf(proLog.get("stage_id"))));
            String statistics = String.valueOf(proLog.get("project_statis"));
            Map<String,Object>  pMap = new HashMap<>();
            pMap.put("log_id",log_id);
            pMap.put("stage_id",stage_id);
            pMap.put("statistics",statistics);
            projectLogDao.assessedLogDetail(pMap);
        }


    }

    /**
     * 获取当日项目统计
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> todayProSum(String id) {
        List<Map<String, Object>> todayProSumList = projectLogDao.todayProSum(id);
        return todayProSumList;
    }


    /**
     * 获取当日项目统计
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getTodayLog(String id) {
        Map<String, Object> todayLog = projectLogDao.getTodayLog(id);
        return todayLog;
    }

    /**
     * 修改日志
     *
     * @param log_id
     * @param param
     */
    @Override
    public void upLog(Object log_id, Map<String, String> param) {
        String project = param.get("project");
        String voice_remark = param.get("voice_remark");
        String characters_remark = param.get("characters_remark");
        Long logid = Long.valueOf(String.valueOf(log_id));

        projectLogDao.upLog(logid,voice_remark,characters_remark);
        projectLogDao.delLogDetail(logid);


        List<Map<String,Object>> proLogList = new Gson().fromJson(project, ArrayList.class);
        for(Map<String,Object> proLog : proLogList){
            Long stage_id = Math.round(Double.parseDouble(String.valueOf(proLog.get("stage_id"))));
            String statistics = String.valueOf(proLog.get("project_statis"));
            Map<String,Object>  pMap = new HashMap<>();
            pMap.put("log_id",log_id);
            pMap.put("stage_id",stage_id);
            pMap.put("statistics",statistics);
            projectLogDao.assessedLogDetail(pMap);
        }
    }
}
